package LeetCode.month01.day31;

import java.util.*;

public class Code {
    public static void main1(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] value = new int[n];
        int[] family = new int[n];
        for (int i = 0; i < n; i++) {
            value[i]=sc.nextInt();
            family[i]=value[i];
        }
        int max = 0;
        for (int i = 0; i < n-1; i++) {
            int fa = sc.nextInt();
            int son = sc.nextInt();
            family[fa-1]+=value[son-1];
            max = Math.max(family[fa-1],max);
        }
        System.out.println(max);
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();

        // 映射id 和财富
        Map<Integer, Integer> idValue = new HashMap<>();
        for (int i = 1; i <= n; i++) {
            int value = sc.nextInt();
            idValue.put(i, value);
        }

        // 映射 id和子节点关系
        Map<Integer, Set<Integer>> childrenMap = new HashMap<>();
        for (int i = 0; i < n - 1; i++) {
            int n1 = sc.nextInt();
            int n2 = sc.nextInt();
            childrenMap.putIfAbsent(n1, new HashSet<>());
            childrenMap.putIfAbsent(n2, new HashSet<>());
            childrenMap.get(n1).add(n2);
        }

        int res = 0;
        // 遍历每个节点计算财富
        for (Map.Entry<Integer, Integer> entry : idValue.entrySet()) {
            int tmp = entry.getValue();
            Set<Integer> children = childrenMap.get(entry.getKey());
            // 额外加上子节点的财富
            if (children != null) {
                for (int id : children) {
                    tmp += idValue.get(id);
                }
            }
            res = Math.max(res, tmp);
        }

        System.out.println(res);
    }
}
